//测试API正式
//const apiUrl = "http://116.205.119.69:8000";

//测试图片地址
//const imgUrl = "http://116.205.119.69:8000/miniapp"

//API正式
const apiUrl = "https://gy.slth.net";
//图片地址
const imgUrl = "https://gy.slth.net/miniapp"



let app = getApp()

// 封装微信请求方法
const request = (params) => {

    let url = params.url;
    let data = params.data;
    let method = params.method;
    let header = {
        "Content-Type": "application/json"
    };

    let token = wx.getStorageSync("token")

    if (token && !params.isLogin) {
        url += "&token="
        url += token
    }


    return new Promise((resolve, reject) => {
        if (method == "POST" && !params.noLoading) {
            wx.showLoading()
        }
        wx.request({
            url: apiUrl + url, // api url
            method: method, // get/post
            data: data, // 请求参数
            header: header, // 头部
            success(result) {
                let res = result.data;
                if (params.outErrMsg) {
                    resolve(res);
                } else if (res.code === "0") {
                    // console.log(res);
                    resolve(res);
                } else if (res.code === "100" && res.msg == "登录失效，请重新登录！") {
                    if (!params.notNeedLogin) {
                        wx.showModal({
                            title: "提示",
                            content: "登录失效",
                            showCancel: false,
                            success(res) {
                                wx.navigateTo({
                                    url: "/pages/Login/Login",
                                });
                            },
                        });
                        reject(res.data);
                    } else {
                        resolve(res.data);
                    }

                } else {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        content: res.msg,
                    })
                    reject(res);
                }
            },
            fail(err) {
                wx.showModal({
                    title: '错误',
                    showCancel: false,
                    content: err.errMsg,
                })
                reject(err);
            },
            complete() {
                if (method == "POST" && !params.noLoading) {
                    wx.hideLoading()
                }
            }
        });
    });
};


module.exports = {
    apiUrl,
    request,
    imgUrl
}